package top.withu.gaof.freehope.config;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author Gaofan
 * @date 2019年3月21日 下午5:52:30
 * @describe 统一异常捕捉
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	// 这个注解是指当controller中抛出这个指定的异常类的时候，都会转到这个方法中来处理异常
	@ExceptionHandler(Exception.class)
	// 将返回的值转成json格式的数据
	@ResponseBody
	// 返回的状态码
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) // 服务内部错误
	public Map<String, Object> handlerUserNotExistException(Exception ex) {
		Map<String, Object> result = new HashMap<String, Object>();
		logger.error("系统内部错误", ex);
		result.put("message", ex.getMessage());
		return result;
	}
	
	
	@ExceptionHandler(ConversionNotSupportedException.class)
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public String handle500Error(){
		return "资源未找到，请确认请求信息";
	}
}
